﻿/*----------------------------------------------------------------------------------------------
*
* This file is XIUSDK's property. It contains XIUSDK's trade secret, proprietary and
* confidential information.
*
* The information and code contained in this file is only for authorized XIUSDK employees
* to design, create, modify, or review.
*
* DO NOT DISTRIBUTE, DO NOT DUPLICATE OR TRANSMIT IN ANY FORM WITHOUT PROPER AUTHORIZATION.
*
* If you are not an intended recipient of this file, you must not copy, distribute, modify,
* or take any action in reliance on it.
*
* If you have received this file in error, please immediately notify XIUSDK and
* permanently delete the original and any copy of any file and any printout thereof.
*
*---------------------------------------------------------------------------------------------*/
/*****************************************************************************
 Copyright:    www.xiusdk.cn(all rights reserved)
 Description:  beautyEngine sdk 包含人脸美化模块/滤镜模块
 Author:       xiusdk
 Version:      V1.2
 qq group:     567648913(加群获取最新信息)
*****************************************************************************/
using UnityEngine;
using Xiu;
using System.Drawing;
using System.IO;
using System;

public class BeautyDemo : MonoBehaviour {

    public Texture2D _mTexture = null;
    public int[] curFacePoints = null;
    public int curFacenum = 0;
    // Use this for initialization
    void Start () {

        curFacePoints = null;
        curFacenum = 0;
        using (BeautyEngine Engine = new BeautyEngine())
        {
            string filePath = Application.dataPath + "/face.png";
            Bitmap bitmap = (Bitmap)System.Drawing.Image.FromFile(filePath);

            using (YNFaceDetector detector = new YNFaceDetector())
            {
                String startup = Application.dataPath;
                detector.loadModels(startup + "/yn_model_detect.tar");

                YNFaceDetector.YNFaces[] result = detector.Detect(bitmap);
                if (result != null && result.Length > 0)
                {
                    YNFaceDetector.YNPoint2f[] facePoints = null;
                    facePoints = result[0].shape.Pts();
                    int ptsSize = result[0].shape.ptsSize;
                    curFacenum = 1;
                    curFacePoints = new int[ptsSize * 2];
                    for (int i = 0; i < ptsSize; i++)
                    {
                        curFacePoints[i * 2 + 0] = (int)facePoints[i].x;
                        curFacePoints[i * 2 + 1] = (int)facePoints[i].y;
                    }
                }
            }
            // set parameters
            // 美肤
            int whitenRatio = 20; 
            // 磨皮
            int softRatio = 80;
            // 滤镜id
            int filterId = 0;
            // 滤镜强度
            int filterRatio = 80;
            // 瘦脸参数
            int faceLiftRatio = 60;
            // 大眼参数
            int eyeWarpRatio = 50;
            // 亮眼参数
            int lightEyeRatio = 45;
            using (Bitmap result = Engine.BeautyProcess(bitmap, whitenRatio, softRatio, filterId, filterRatio, curFacenum, curFacePoints, faceLiftRatio, eyeWarpRatio, lightEyeRatio))
            {
                bitmap.Dispose();
                bitmap = null;

                _mTexture = new Texture2D(result.Width, result.Height);
                using (MemoryStream ms = new MemoryStream())
                {
                    result.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    _mTexture.LoadImage(ms.ToArray());
                }
            }
        }
    }

    void OnGUI()
    {
        GUILayout.Label(_mTexture, GUILayout.Width(512), GUILayout.Height(512));
    }
    // Update is called once per frame
    void Update () {

    }
}